// 对网络请求封装
// const app1 = getApp()

const http = (url, data = {}, method = 'get') => {
	let pach = 'http://estate.eshareedu.cn/estate' + url
	return new Promise((resolve, reject) => {
		wx.request({
			url: pach,
			data,
			method,
			header: { // 默认值
				Authorization: uni.getStorageSync('token')
			},
			success: (res) => {
				if (res.statusCode !== 200) {
					uni.showToast({
						title: res.errMsg
					})
				} else {
					// 10200 没有权限访问
					if (res.data.errCode === 10200) {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}
				}
				resolve(res.data)
			},
			complete: res => {
				// '请求成功和失败都走这个回调'
				uni.hideLoading()
			},
			fail: (err) => {
				uni.showToast({
					title: '网络连接失败，请重试',
					icon: 'none'
				})
				console.log(err.errMsg);
			}
		})
	})
}
const get = (url, data) => http(url, data, 'get')
const post = (url, data) => http(url, data, 'post')

module.exports = {
	get,
	post
}
